package com.treehouse.gui.actorview;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.treehouse.gui.ViewControllerComponent;
import com.treehouse.logging.Logger;
import com.treehouse.pornstar.Pornstar;
import com.treehouse.pornstar.services.IPornstarServices;
import com.treehouse.service.application.IApplicationService;

public class ActorViewController extends ViewControllerComponent implements IActorViewController, ActionListener{

	private IPornstarServices pornstarServices;
	
	private JPanel topPan;
	private JPanel selectPan;
	private JComboBox actorSelect;
	private JButton reloadActor;
	private Thread reloadActorThread;
	
	private JButton reloadScenes;
	private JButton reloadAll;
	private JPanel picturePan;
	private JLabel imageView;
	
	private JPanel detailPan;
	private JPanel labelPan;
	private JLabel name;
	private JLabel rating;
	private JLabel scenes;
	private JLabel age;
	private JTextArea description;
	

	
	public ActorViewController(IApplicationService applicationService, IPornstarServices pornstarServices) {
		super(applicationService);
		this.pornstarServices = pornstarServices;
		this.initializeComponents();
		this.doLayout();
		this.loadActorData();
	}
	
	private void initializeComponents(){
		this.topPan = new JPanel();
		this.selectPan = new JPanel();
		this.actorSelect = new JComboBox();
		this.reloadActor = new JButton("Reload actors");
		this.reloadActor.addActionListener(this);
		this.reloadScenes = new JButton("Reload scenes");
		this.reloadAll = new JButton("Reload all");
		this.picturePan = new JPanel();
		this.imageView = new JLabel(new ImageIcon("ressources\\brazzers.png"));
		this.detailPan = new JPanel();
		this.labelPan = new JPanel();
		this.name = new JLabel("Name: ");
		this.rating = new JLabel("Rating: ");
		this.scenes = new JLabel("Scenes: ");
		this.age = new JLabel("Age: ");
		this.description = new JTextArea("No description available");
	}
	
	private void doLayout(){
		this.topPan.setLayout(new GridLayout(1,2));
		this.selectPan.setLayout(new GridLayout(4,1));
		this.topPan.add(this.selectPan);
		this.topPan.add(this.picturePan);
		this.selectPan.add(this.actorSelect);
		this.selectPan.add(this.reloadActor);
		this.selectPan.add(this.reloadScenes);
		this.selectPan.add(this.reloadAll);
	//	this.picturePan.add(this.imageView);
		
		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints gbcSmall = new GridBagConstraints();
		GridBagConstraints gbcBig = new GridBagConstraints();
		gbcBig.gridx = 0;
		gbcBig.gridy = 1;
		gbcSmall.gridx = 0;
		gbcBig.weightx = 0.9;
		gbcBig.weighty = 0.9;
		gbcSmall.weightx = 0.1;
		gbcSmall.weighty = 0.1;
		gbcSmall.gridy = 0;
		gbcBig.fill = GridBagConstraints.BOTH;
		gbcSmall.fill = GridBagConstraints.BOTH;
		
		GridBagLayout gblView = new GridBagLayout();
		GridBagConstraints gbcSmallView = new GridBagConstraints();
		GridBagConstraints gbcBigView = new GridBagConstraints();
		gbcBigView.gridx = 0;
		gbcBigView.gridy = 1;
		gbcSmallView.gridx = 0;
		gbcBigView.weightx = 0.9;
		gbcBigView.weighty = 0.9;
		gbcSmallView.weightx = 0.1;
		gbcSmallView.weighty = 0.1;
		gbcSmallView.gridy = 0;
		gbcBigView.fill = GridBagConstraints.BOTH;
		gbcSmallView.fill = GridBagConstraints.BOTH;
		

		
		this.description.setBorder(BorderFactory.createTitledBorder("Description"));
		this.description.setLineWrap(true);
		this.description.setWrapStyleWord(true);
		JScrollPane pane = new JScrollPane(this.description);
		this.description.setEditable(false);
		gbl.addLayoutComponent(this.labelPan, gbcSmall);
		gbl.addLayoutComponent(pane, gbcBig);
		this.detailPan.add(labelPan);
		this.detailPan.add(pane);
		this.detailPan.setLayout(gbl);
		
		this.labelPan.setLayout(new GridLayout(2,2));
		this.labelPan.add(this.name);
		this.labelPan.add(this.rating);
		this.labelPan.add(this.scenes);
	//	this.labelPan.add(this.age);
		
		gblView.addLayoutComponent(this.topPan, gbcSmallView);
		gblView.addLayoutComponent(this.detailPan, gbcBigView);
		this.getView().setLayout(gblView);
		this.getView().add(this.topPan);
		this.getView().add(this.detailPan);
		
		this.actorSelect.addActionListener(this);
		
		
	}
	
	public Pornstar getSelectedActor(){
		Object item = this.actorSelect.getSelectedItem();
		Pornstar actor = null;
		if(item != null && item instanceof Pornstar){
			actor = (Pornstar) item;
		}
		return actor;
	}
	
	private void loadActorData(){
		Pornstar[] list = this.pornstarServices.getPornstars();
		if(list.length < 10){
			Logger.inf("Pornstar Data out of date, reload recomended.");
		}
		this.actorSelect.removeAllItems();
		for(Pornstar star : list){
			this.actorSelect.addItem(star);
		}
	}
	
	

	@Override
	public void actionPerformed(ActionEvent arg0) {
		String command = arg0.getActionCommand();
		
		if(command.equals("Reload actors")){
			reloadActorThread = new Thread(){
				public void run(){
					Logger.inf("Reloading Actordata..");
					LinkedList<Pornstar> list = pornstarServices.reloadPornstarListToDB();
					actorSelect.removeAllItems();
					for(Pornstar actor : list){
						actorSelect.addItem(actor);
					}
					Logger.inf("Finished!");
				}
			};
			this.reloadActor.setText("Stop reloading");
			this.reloadActorThread.start();
		
		}
		
		else if(command.equals("comboBoxChanged")){
			Thread t = new Thread(){
				public void run(){
					Pornstar actor = getSelectedActor();
					Logger.inf("Selected Actor " + actor.getName());
					name.setText("Name: " + actor.getName());
					rating.setText("Rating: " + actor.getRating());
					scenes.setText("Scenes: 0");
					age.setText("Age: 0");
					if(actor.getDescription().length() > 10){
					description.setText(actor.getDescription());
					}
					else description.setText("No description available");
				}
			};
			t.start();
		}
		
		else if(command.equals("Stop reloading")){
			this.reloadActorThread.interrupt();
			this.reloadActor.setText("Reload actors");
		}
	}
	
	
	
	
	
	


}
